import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:ball_lion/service/config_service.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/utils/widget_utils.dart';
import 'package:ball_lion/widgets/base/wqs_widget.dart';
import 'package:ball_lion/widgets/open_notification_dialog.dart';

import 'package:ball_lion/widgets/base/wqs_text.dart';

class ZqPushConfigPage extends StatefulWidget {
  const ZqPushConfigPage({super.key});

  @override
  State<ZqPushConfigPage> createState() => _ZqPushConfigPageState();
}

class _ZqPushConfigPageState extends State<ZqPushConfigPage> {
  ZqConfig config = Get.find<ConfigService>().zqConfig;
  PushConfig pushConfig = Get.find<ConfigService>().pushConfig;
  ConfigService configService = Get.find<ConfigService>();
  bool showPushAll = false;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (configService.pushConfig.pushAll == 0) {
      showPushAll = true;
    }
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
      final permission = await Permission.notification.isGranted;
      if (!permission) {
        Get.dialog(const OpenNotificationDialog());
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Styles.appBar(title: QsText('足球比赛通知')),
      backgroundColor: Colours.scaffoldBg,
      body: Container(
        padding: const EdgeInsets.symmetric(horizontal: 8),
        child: ListView(children: [
          const SizedBox(height: 10),
          if (showPushAll) ...[
            QsWidget.switchCell('消息通知设置', pushConfig.pushAll == 1, onTap: () {
              pushConfig.pushAll = (pushConfig.pushAll == 1 ? 0 : 1);
              update();
              configService.update(ConfigType.pushAll, pushConfig.pushAll);
            }),
            separateTitle('开启后，您可以接收到最新动态消息'),
          ],
          if (pushConfig.pushAll == 1) ...[
            QsWidget.switchCell('关注足球比赛', config.zqPush1 == 1, onTap: () {
              config.zqPush1 = (config.zqPush1 == 1 ? 0 : 1);
              update();
              configService.update(ConfigType.zqPush1, config.zqPush1);
            }),
            separateTitle('开启后，您可以接收到已关注比赛的最新消息动态'),
            if (config.zqPush1 == 1) soccerPushConfig()
          ]
        ]),
      ),
    );
  }

  Widget soccerPushConfig() {
    return Styles.card(
        color: Colours.white,
        child: Column(children: [
          QsWidget.switchCell('首发阵容', config.zqPush2 == 1, isCard: false,
              onTap: () {
            config.zqPush2 = (config.zqPush2 == 1 ? 0 : 1);
            update();
            configService.update(ConfigType.zqPush2, config.zqPush2);
          }),
          QsWidget.seperateLine(),
          QsWidget.switchCell('开赛（赛前5分钟）', config.zqPush3 == 1, isCard: false,
              onTap: () {
            config.zqPush3 = (config.zqPush3 == 1 ? 0 : 1);
            update();
            configService.update(ConfigType.zqPush3, config.zqPush3);
          }),
          QsWidget.seperateLine(),
          QsWidget.switchCell('赛果', config.zqPush4 == 1, isCard: false,
              onTap: () {
            config.zqPush4 = (config.zqPush4 == 1 ? 0 : 1);
            update();
            configService.update(ConfigType.zqPush4, config.zqPush4);
          }),
          QsWidget.seperateLine(),
          QsWidget.switchCell('进球', config.zqPush5 == 1, isCard: false,
              onTap: () {
            config.zqPush5 = (config.zqPush5 == 1 ? 0 : 1);
            update();
            configService.update(ConfigType.zqPush5, config.zqPush5);
          }),
          QsWidget.seperateLine(),
          QsWidget.switchCell('红牌', config.zqPush6 == 1, isCard: false,
              onTap: () {
            config.zqPush6 = (config.zqPush6 == 1 ? 0 : 1);
            update();
            configService.update(ConfigType.zqPush6, config.zqPush6);
          }),
          QsWidget.seperateLine(),
          QsWidget.switchCell('黄牌', config.zqPush7 == 1, isCard: false,
              onTap: () {
            config.zqPush7 = (config.zqPush7 == 1 ? 0 : 1);
            update();
            configService.update(ConfigType.zqPush7, config.zqPush7);
          }),
          QsWidget.seperateLine(),
          QsWidget.switchCell('角球', config.zqPush8 == 1, isCard: false,
              onTap: () {
            config.zqPush8 = (config.zqPush8 == 1 ? 0 : 1);
            update();
            configService.update(ConfigType.zqPush8, config.zqPush8);
          }),
        ]));
  }

  Widget separateTitle(String title) {
    return Padding(
      padding: const EdgeInsets.only(left: 16, top: 6, bottom: 12),
      child: QsText(title, color: Colours.grey66, fontSize: 13),
    );
  }
}
